﻿using System;
using System.Net;
using SCG.General;

namespace SCG.SolarVengeanceEngine.StarShips
{
    //Elites
    public class Elite : WarShip
    {
        //Constructor
        public Elite(SVGame game, Player owner, int engines, int weapons, StarSystem origin)
            : base(game, owner, engines, weapons, origin)
        {
        }

        //Ship name
        public override string Name
        {
            get
            {
                return "Elite";
            }
        }

        //description
        public override string Description
        {
            get
            {
                return "Elites are WarShips whose Weapon rating advances as they destroy enemy StarShips.  Also, cloaked Elites remain invisible to your Allies.";
            }
        }

        //ship type
        public override StarShipType ShipType
        {
            get
            {
                return StarShipType.Elite;
            }
        }

        //dont pass visibility to allies
        public override bool PassesVisibilityToAllies
        {
            get
            {
                return !Cloaked;
            }
        }

        //Kill points track how much the Elite needs to advance in level
        public int KillPoints { get; set; }

        //advance after destroying an opponent
        protected override void TargetAdjacentStarShip(StarShip target)
        {
            base.TargetAdjacentStarShip(target);
            if (target.Destroyed)
            {
                int buildCostElite = ShipCostCalculator.GetCost(StarShipType.Elite, MaxEngines, MaxValue, Cloaked, null);
                int buildCostNextLevel = ShipCostCalculator.GetCost(StarShipType.Elite, MaxEngines, MaxValue + 1, Cloaked, null);
                int buildCostTarget = ShipCostCalculator.GetCost(target.ShipType, target.MaxEngines, target.MaxValue, target.Cloaked, null);
                int differential = buildCostNextLevel - buildCostElite;
                KillPoints += buildCostTarget;
                Game.SubmitStateChange(StateChange.SetKillPoints, this, KillPoints);
                if (KillPoints >= differential)
                {
                    Value++;
                    MaxValue++;
                    KillPoints -= differential;
                    Game.SubmitStateChange(StateChange.SetKillPoints, this, KillPoints);
                    Game.SubmitStateChange(StateChange.SetShipValues, this, Engines, Value);                 
                }
            }
        }

        //Persistence
        protected override string SubClassPersist()
        {
            Tokenizer tok = new Tokenizer();
            tok.AppendToken(KillPoints);
            return base.SubClassPersist() + tok.Result;
        }
        protected override void SubClassParse(SVTokenizer tok)
        {
            base.SubClassParse(tok);
            KillPoints = tok.TokenInt;            
        }
    }
}
